/*
 * Login.java
 *
 * Created on 10 de Setembro de 2006, 19:42
 */

package tudolist.gui;

import java.util.List;

import javax.swing.JOptionPane;

import org.jgenesis.bd.BasicBD;
import org.jgenesis.context.JGLocalContext;
import org.jgenesis.context.JGRequest;
import org.jgenesis.context.JGRequestImpl;
import org.jgenesis.context.JGResponse;

import tudolist.bean.User;
import tudolist.gui.action.ExitAction;

/**
 *
 * @author  lucas
 */
public class Login extends javax.swing.JFrame {
    
    /** Creates new form Login */
    public Login() {
	initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabelLogo = new javax.swing.JLabel();
        jLabelUsername = new javax.swing.JLabel();
        jGTextFieldUsername = new org.jgenesis.swing.JGTextField();
        jLabelPassword = new javax.swing.JLabel();
        jGTextFieldPassword = new org.jgenesis.swing.JGTextField();
        jGButtonPanel = new org.jgenesis.swing.JGButtonPanel();
        jButtonExit = new javax.swing.JButton();
        jButtonLogin = new javax.swing.JButton();
        jButtonLogin.setIcon(Images.ENTER);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("TuduList");
        setResizable(false);
        jLabelLogo.setIcon(new javax.swing.ImageIcon("/home/lucas/projetos/java/todolist/docs/banner.png"));
        jLabelLogo.setText("Icon");

        jLabelUsername.setLabelFor(jGTextFieldUsername);
        jLabelUsername.setText("Username");

        jLabelPassword.setLabelFor(jLabelPassword);
        jLabelPassword.setText("Password");

        jGTextFieldPassword.setPasswordEnable(true);

        jButtonExit.setAction(new ExitAction());

        jButtonLogin.setText("Enter");
        jButtonLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLoginActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jGButtonPanelLayout = new org.jdesktop.layout.GroupLayout(jGButtonPanel);
        jGButtonPanel.setLayout(jGButtonPanelLayout);
        jGButtonPanelLayout.setHorizontalGroup(
            jGButtonPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jGButtonPanelLayout.createSequentialGroup()
                .add(jButtonLogin, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButtonExit, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE))
        );
        jGButtonPanelLayout.setVerticalGroup(
            jGButtonPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
            .add(jButtonLogin, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .add(jButtonExit, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabelLogo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 177, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabelUsername)
                    .add(jLabelPassword)
                    .add(jGTextFieldUsername, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                    .add(jGTextFieldPassword, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                    .add(jGButtonPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabelLogo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(jLabelUsername)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jGTextFieldUsername, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabelPassword)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jGTextFieldPassword, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 66, Short.MAX_VALUE)
                        .add(jGButtonPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-398)/2, (screenSize.height-230)/2, 398, 230);
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLoginActionPerformed
    	String username = jGTextFieldUsername.getValue().toString();
    	String password = jGTextFieldPassword.getValue().toString();
    	
    	JGRequest findUser = new JGRequestImpl("find");
    	findUser.addParameter("queryName", "authenticate");
    	findUser.addParameter("name", username);
    	findUser.addParameter("password", password);
    	
    	JGResponse response = BasicBD.getInstance().doRequest(findUser);
    	
    	List result = (List) response.getParameter("findResult");
    	if (result == null) {
    		JOptionPane.showMessageDialog(this, "Invalid login!");
    	} else {
    		User user = (User) result.get(0);
    		JGLocalContext.getInstance().addAttribute("user", user);
    	}
    }//GEN-LAST:event_jButtonLoginActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
	java.awt.EventQueue.invokeLater(new Runnable() {
	    public void run() {
		new Login().setVisible(true);
	    }
	});
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonExit;
    private javax.swing.JButton jButtonLogin;
    private org.jgenesis.swing.JGButtonPanel jGButtonPanel;
    private org.jgenesis.swing.JGTextField jGTextFieldPassword;
    private org.jgenesis.swing.JGTextField jGTextFieldUsername;
    private javax.swing.JLabel jLabelLogo;
    private javax.swing.JLabel jLabelPassword;
    private javax.swing.JLabel jLabelUsername;
    // End of variables declaration//GEN-END:variables
    
}
